﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using Wjtour.Common;

namespace Wjtour.BLL
{
    public class sitepicture_bll : base_dll
    {
        public List<Wjtour.Domain.sitepicture> GetTopList(string type, int top)
        {
            string where = " where 1=1";
            if (!string.IsNullOrEmpty(type))
                where += " and a.picturetypeid in (" + type + ")";

            string sql = string.Format(@"select top {0} a.* from sitepicture a
                {1}
                order by a.id desc",top, where);
            return GetAll<Wjtour.Domain.sitepicture>(sql).ToList();
        }

        public List<Wjtour.Domain.sitepicture> GetMemberPicturesList(string type, int memberid)
        {
            string where = " where 1=1";
            if (!string.IsNullOrEmpty(type))
                where += " and a.picturetypeid in (" + type + ")";
            if (memberid > 0)
                where += " and a.memberid =" + memberid;

            string sql = string.Format(@"select a.* from sitepicture a
                {0}
                order by a.id desc", where);
            return DbAccess.GetObjects<Wjtour.Domain.sitepicture>(sql).ToList();
        }

        /// <summary>
        /// 获取某个会员发布的某种类型的图片
        /// </summary>
        /// <param name="type"></param>
        /// <param name="memberid"></param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public PaginationData<Wjtour.Domain.sitepicture> GetList(string type, int memberid, int page, int pagesize = 20)
        {
            string where = " where 1=1";
            if (!string.IsNullOrEmpty(type))
                where += " and a.picturetypeid in (" + type + ")";
            if (memberid > 0)
                where += " and a.memberid =" + memberid;

            string sql = string.Format(@"select a.* from sitepicture a
                {0}
                order by a.id desc", where);
            PaginationData<Wjtour.Domain.sitepicture> data = GetPaginationData<Wjtour.Domain.sitepicture>(sql, page, pagesize);
            return data;
        }

        public PaginationData<Wjtour.Domain.dto.sitepicture_picturetype> GetList(string type, int page, int pagesize = 20)
        {
            string where = " where 1=1";
            if (!string.IsNullOrEmpty(type))
                where += " and a.picturetypeid in (" + type + ")";

            string sql = string.Format(@"select a.*,b.picturetypename,g.title galleryname,g.createdate galleryaddtime from sitepicture a
                inner join picturetype b on a.picturetypeid = b.id
                left join gallery g on a.galleryid = g.id
                {0}
                order by a.id desc", where);
            PaginationData<Wjtour.Domain.dto.sitepicture_picturetype> data = GetPaginationData<Wjtour.Domain.dto.sitepicture_picturetype>(sql, page, pagesize);
            return data;
        }

        public PaginationData<Wjtour.Domain.dto.sitepicture_picturetype> GetCoverList(string type, int page, int pagesize = 20)
        {
            string where = " where 1=1";
            if (!string.IsNullOrEmpty(type))
                where += " and a.picturetypeid in (" + type + ") and a.iscover = 1";

            string sql = string.Format(@"select a.*,b.picturetypename,g.title galleryname,g.createdate galleryaddtime from sitepicture a
                inner join picturetype b on a.picturetypeid = b.id
                left join gallery g on a.galleryid = g.id
                {0}
                order by a.id desc", where);
            PaginationData<Wjtour.Domain.dto.sitepicture_picturetype> data = GetPaginationData<Wjtour.Domain.dto.sitepicture_picturetype>(sql, page, pagesize);
            return data;

        }
        public Wjtour.Domain.sitepicture Load(int id)
        {
            string sql = "select * from sitepicture where id=" + id;
            return DbAccess.GetObjects<Wjtour.Domain.sitepicture>(sql).FirstOrDefault();
        }

        public bool Save(Wjtour.Domain.sitepicture entity)
        {
            string sql = "";

            DbCommand cmd = DbAccess.CreateCommand();

            if (entity.id > 0)
            {
                sql = @"UPDATE sitepicture SET 
                picturetitle = @picturetitle,
                picturedesc = @picturedesc,
                picturetypeid = @picturetypeid,
                picturepath = @picturepath,
                linkpath = @linkpath,
                openmode = @openmode,
                sort = @sort,
                createdate = @createdate,
                isactive = @isactive,
                tag = @tag,
                memberid = @memberid,
                isaudit = @isaudit,
                smallImg = @smallImg,
                galleryid = @galleryid,
                iscover = @iscover,
                zan = @zan,
                rar = @rar
                 WHERE id=@id";
                DbAccess.AddInParameter(cmd, "@id", DbType.Int32, entity.id);

            }
            else
            {
                sql = @"INSERT INTO sitepicture( 
                    picturetitle,picturedesc,picturetypeid,picturepath,linkpath,openmode,sort,createdate,isactive,tag,memberid,isaudit,smallImg,galleryid,iscover,zan,rar)VALUES(
                    @picturetitle,@picturedesc,@picturetypeid,@picturepath,@linkpath,@openmode,@sort,@createdate,@isactive,@tag,@memberid,@isaudit,@smallImg,@galleryid,@iscover,@zan,@rar)";
            }
            DbAccess.AddInParameter(cmd, "@picturetitle", DbType.String, entity.picturetitle);
            DbAccess.AddInParameter(cmd, "@picturedesc", DbType.String, entity.picturedesc);
            DbAccess.AddInParameter(cmd, "@picturetypeid", DbType.Int32, entity.picturetypeid);
            DbAccess.AddInParameter(cmd, "@picturepath", DbType.String, entity.picturepath);
            DbAccess.AddInParameter(cmd, "@linkpath", DbType.String, entity.linkpath);
            DbAccess.AddInParameter(cmd, "@openmode", DbType.String, entity.openmode);
            DbAccess.AddInParameter(cmd, "@sort", DbType.Int32, entity.sort);
            DbAccess.AddInParameter(cmd, "@createdate", DbType.DateTime, entity.createdate);
            DbAccess.AddInParameter(cmd, "@isactive", DbType.Int32, entity.isactive);
            DbAccess.AddInParameter(cmd, "@tag", DbType.String, entity.tag);
            DbAccess.AddInParameter(cmd, "@memberid", DbType.Int32, entity.memberid);
            DbAccess.AddInParameter(cmd, "@isaudit", DbType.Int32, entity.isaudit);
            DbAccess.AddInParameter(cmd, "@smallImg", DbType.String, entity.smallImg);
            DbAccess.AddInParameter(cmd, "@galleryid", DbType.Int32, entity.galleryid);
            DbAccess.AddInParameter(cmd, "@iscover", DbType.Int32, entity.iscover);
            DbAccess.AddInParameter(cmd, "@zan", DbType.Int32, entity.zan);
            DbAccess.AddInParameter(cmd, "@rar", DbType.String, entity.rar);
            cmd.CommandText = sql;
            cmd.CommandType = System.Data.CommandType.Text;
            return DbAccess.ExecuteNonQuery(cmd) > 0;
        }

        public bool Save(Wjtour.Domain.sitepicture entity, out int id)
        {
            string sql = "";

            DbCommand cmd = DbAccess.CreateCommand();

            if (entity.id > 0)
            {
                sql = @"UPDATE sitepicture SET 
                picturetitle = @picturetitle,
                picturedesc = @picturedesc,
                picturetypeid = @picturetypeid,
                picturepath = @picturepath,
                linkpath = @linkpath,
                openmode = @openmode,
                sort = @sort,
                createdate = @createdate,
                isactive = @isactive,
                tag = @tag,
                memberid = @memberid,
                isaudit = @isaudit,
                smallImg = @smallImg,
                galleryid = @galleryid,
                iscover = @iscover,
                zan = @zan,
                rar = @rar
                 WHERE id=@id";
                DbAccess.AddInParameter(cmd, "@id", DbType.Int32, entity.id);

            }
            else
            {
                sql = @"INSERT INTO sitepicture( 
                    picturetitle,picturedesc,picturetypeid,picturepath,linkpath,openmode,sort,createdate,isactive,tag,memberid,isaudit,smallImg,galleryid,iscover,zan,rar)VALUES(
                    @picturetitle,@picturedesc,@picturetypeid,@picturepath,@linkpath,@openmode,@sort,@createdate,@isactive,@tag,@memberid,@isaudit,@smallImg,@galleryid,@iscover,@zan,@rar);
                    select @id = @@IDENTITY";
            }
            DbAccess.AddInParameter(cmd, "@picturetitle", DbType.String, entity.picturetitle);
            DbAccess.AddInParameter(cmd, "@picturedesc", DbType.String, entity.picturedesc);
            DbAccess.AddInParameter(cmd, "@picturetypeid", DbType.Int32, entity.picturetypeid);
            DbAccess.AddInParameter(cmd, "@picturepath", DbType.String, entity.picturepath);
            DbAccess.AddInParameter(cmd, "@linkpath", DbType.String, entity.linkpath);
            DbAccess.AddInParameter(cmd, "@openmode", DbType.String, entity.openmode);
            DbAccess.AddInParameter(cmd, "@sort", DbType.Int32, entity.sort);
            DbAccess.AddInParameter(cmd, "@createdate", DbType.DateTime, entity.createdate);
            DbAccess.AddInParameter(cmd, "@isactive", DbType.Int32, entity.isactive);
            DbAccess.AddInParameter(cmd, "@tag", DbType.String, entity.tag);
            DbAccess.AddInParameter(cmd, "@memberid", DbType.Int32, entity.memberid);
            DbAccess.AddInParameter(cmd, "@isaudit", DbType.Int32, entity.isaudit);
            DbAccess.AddInParameter(cmd, "@smallImg", DbType.String, entity.smallImg);
            DbAccess.AddInParameter(cmd, "@galleryid", DbType.Int32, entity.galleryid);
            DbAccess.AddInParameter(cmd, "@iscover", DbType.Int32, entity.iscover);
            DbAccess.AddInParameter(cmd, "@zan", DbType.Int32, entity.zan);
            DbAccess.AddInParameter(cmd, "@rar", DbType.String, entity.rar);
            cmd.CommandText = sql;
            cmd.CommandType = System.Data.CommandType.Text;
            bool ret = DbAccess.ExecuteNonQuery(cmd) > 0;
            if (ret)
            {
                id = Convert.ToInt32(cmd.Parameters["@id"].Value);
            }
            else id = 0;
            return ret;
        }


        public int BatchDelete(string ids)
        {
            string sql = "delete from sitepicture where id in(" + ids + ")";
            return DbAccess.ExecuteNonQuery(sql);
        }

        /// <summary>
        /// 批量审核
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="audit"></param>
        /// <returns></returns>
        public int BatchAudit(string ids, int audit)
        {
            string sql = "update sitepicture set isaudit = " + audit + " where id in(" + ids + ")";
            return DbAccess.ExecuteNonQuery(sql);
        }
        /// <summary>
        /// 将某一个图片设为封面
        /// </summary>
        /// <param name="picid"></param>
        /// <returns></returns>
        public int SetCover(int picid)
        {
            string sql = string.Format(@"
            declare @gid int;
            select @gid = galleryid from sitepicture sp where sp.id={0}
            update sitepicture set iscover = 0 where galleryid = @gid
            update sitepicture set iscover = 1 where id = {0}
            ", picid);

            return DbAccess.ExecuteNonQuery(sql);
        }
    }
}
